package xhwl.insurance.ui;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import xhwl.android.common.util.StringUtil;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.dialog.VideoShareDialog;
import xhwl.insurance.http.bean.resp.HospitalListResp;

/**
 * Created by fuyi on 2017/11/23.
 */

public class HospitalDetailsActivity extends BaseActivity implements View.OnClickListener {
    private VideoShareDialog shareDialog;
    private String url;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospitaldetails);
    }


    @Override
    public void initViews() {
        initTitleViews();
        TextView mRegion = (TextView) findViewById(R.id.tv_region_hd);
        TextView mGrade = (TextView) findViewById(R.id.tv_grade_hd);
        TextView mCategory = (TextView) findViewById(R.id.tv_category_hd);
        TextView mNature = (TextView) findViewById(R.id.tv_nature_hd);
        TextView mHospitalName = (TextView) findViewById(R.id.tv_hospitalname_hd);
        TextView mHospitalDescribe = (TextView) findViewById(R.id.tv_hospitaldescribe_hd);
        TextView mHospitalAddress = (TextView) findViewById(R.id.tv_hospitaladdress_hd);
        ImageView mPic = (ImageView) findViewById(R.id.iv_pic_hds);

        rightImageView.setVisibility(View.VISIBLE);
        rightImageView.setImageDrawable(getResources().getDrawable(R.drawable.icon_share2));

        Intent intent = getIntent();
        mRegion.setText(intent.getStringExtra("DISTRICT_NAME"));
        mGrade.setText(intent.getStringExtra("LEVEL_NAME"));
        mCategory.setText(intent.getStringExtra("TYPE_NAME"));
        mNature.setText(intent.getStringExtra("HOS_XZ"));
        mHospitalAddress.setText("                    "+intent.getStringExtra("HOS_ADDRESS"));
        mHospitalName.setText(intent.getStringExtra("HOS_NAME"));

        Glide.with(HospitalDetailsActivity.this).load(intent.getStringExtra("HOS_PIC"))
                .error(R.drawable.icon_fail).into(mPic);

        url = "http://weixin-ygj.yk2020.com/weixin/index.php?m=Member&c=Jd&a=detail&hos_id="
                +intent.getStringExtra("HOS_ID");

        String hos_desc = intent.getStringExtra("HOS_DESC");
        String replace = hos_desc.replace("\n", "\n");
        mHospitalDescribe.setText("                    " +replace);

    }

    @Override
    protected void setUpTitleView() {
        titleTextView.setText(getIntent().getStringExtra("HOS_NAME"));
        // TODO Auto-generated method stub
        super.setUpTitleView();
    }

    @Override
    public void initListener() {
        rightImageView.setOnClickListener(this);

    }

    @Override
    public void loadData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_right_imageview:
                shareDialog = new VideoShareDialog(HospitalDetailsActivity.this);
                shareDialog.setCancelable(false);
                shareDialog.setCanceledOnTouchOutside(false);
                View dialog = View.inflate(HospitalDetailsActivity.this, R.layout.dialog_videoshare, null);
                RelativeLayout mWxCircle = (RelativeLayout) dialog.findViewById(R.id.rl_wxcircle);
                RelativeLayout mWechat = (RelativeLayout) dialog.findViewById(R.id.rl_wechat);
                RelativeLayout mCopy = (RelativeLayout) dialog.findViewById(R.id.rl_copy);
                TextView mCancel = (TextView) dialog.findViewById(R.id.btn_videocancel);
                shareDialog.setContentView(dialog);
                shareDialog.show();

                final UMImage image = new UMImage(HospitalDetailsActivity.this, getIntent().getStringExtra("HOS_PIC"));
                mWxCircle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new ShareAction(HospitalDetailsActivity.this)
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                                .setCallback(shareListener)
                                .withTitle(getIntent().getStringExtra("HOS_NAME"))
                                .withTargetUrl(url)
                                .withText(url)
                                .withMedia(image)
                                .share();

                    }
                });
//
                mWechat.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new ShareAction(HospitalDetailsActivity.this)
                                .setPlatform(SHARE_MEDIA.WEIXIN)
                                .setCallback(shareListener)
                                .withTitle(getIntent().getStringExtra("HOS_NAME"))
                                .withTargetUrl(url)
                                .withText(url)
                                .withMedia(image)
                                .share();
                    }
                });

                mCopy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!StringUtil.isEmpty(url)) {
                            ClipboardManager cmb = (ClipboardManager) HospitalDetailsActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                            cmb.setText(url);
                            ToastUtil.showToast(HospitalDetailsActivity.this, "链接已复制");
                        } else {
                            ToastUtil.showToast(HospitalDetailsActivity.this, "该医院没有链接");
                        }

                    }
                });

                mCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shareDialog.dismiss();
                    }
                });

                break;
        }
    }


    private UMShareListener shareListener = new UMShareListener() {

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtil.showToast(HospitalDetailsActivity.this, "分享成功！");
            if (shareDialog != null && shareDialog.isShowing()) {
                shareDialog.dismiss();
            }
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtil.showToast(HospitalDetailsActivity.this, "分享失败！");
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtil.showToast(HospitalDetailsActivity.this, "取消分享");

        }
    };

}
